package ar.edu.unq.sandsoft.utils;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class ParsingHelper {

    public Object toObject(byte[] bytedObject) {
        Object object = null;
        try {
            object = new ObjectInputStream(new ByteArrayInputStream(bytedObject)).readObject();
        } catch (Exception e) {
            throw new RuntimeException("parsing error");
        }
        return object;
    }

    public byte[] toBytes(Object object) {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutputStream oos;
        byte[] data;
        try {
            oos = new ObjectOutputStream(bos);
            oos.writeObject(object);
            oos.flush();
            oos.close();
            bos.close();
            data = bos.toByteArray();
        } catch (IOException e) {
            throw new UnsupportedOperationException();
        }
        return data;
    }

    public String adaptClassName(String fullClassName) {
        return fullClassName.substring(fullClassName.lastIndexOf(".") + 1);
    }

}
